<?php
namespace c\ex\u;

/**
 * Description of relation
 *
 * @author YeFng
 */
class relation extends \control
{
	var $auth_m = 'ex\u\relation';
	var $class = array('PU'=>'\c\auth\permission');
    
	//获取推荐层次
	function gn()
    {
        $g = include(CONFIG_DIR . 'ex.php');
        return $g['user_relation_g'];
    }
	
    function index_home()
    {
        $s = $this->P_G('s');
		$t = array('user'=>'',
                    'sub'=>array('as'=>'user','on'=>'sub.parent_id = user.id', 'join'=>'LEFT JOIN'));
        $w = '';
		
        if (!$this->PU->check($this->auth_m, TRUE))
         {
            if ($s['user_name'])
             {
				$pid = $sid = \YF_f::initC ('\c\user\g')->nameToID($s['user_name']);
                
                $g = $this->gn();
				while ($pid!=\YF_f::$login_user_id)
				 {
					if($g==0 or !$pid)
                        $this->clue (\lang::g('action deny'));
					
					$pid = $this->db->getOneField('user','parent_id','id='.$pid);
					$g--;
				 }				
				$w .= ' AND user.id ='. $sid;
             }
			elseif ($s['p_id'])
				$w .= ' AND user.parent_id ='. $this->PU->u_de($s['p_id']);
            else
                $w .= ' AND user.id ='. \YF_f::$login_user_id;
         }
		else{
			if ($s['user_name'])
				$w .= ' AND user.id ='. \YF_f::initC ('\c\user\g')->nameToID($s['user_name']);
			elseif ($s['p_id'])
				$w .= ' AND user.parent_id ='. $this->PU->u_de($s['p_id']);
		 }
		 
        if ($s['month'])
            $w .= ' AND user.reg_time like "'. $this->db->escape($s['month']) .'%"';
		
        $w && $w = substr ($w, 4);
        $res = $this->db->group('user.id')->get_page(array(), $t, 'user.*, count(sub.id) sub_userNum', $w, 'id DESC');
        $this->loadTemplate('ex/relation', array('res'=>$res, 's'=>$s));
		$this->display('user');
    }
}
